﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
namespace UI
{
    public class UIHeroPage :Page
    {
        public UIHeroLogic logic;

        public TweenAlpha bodyAlpha;
        public TweenPosition titlePos;
        public TweenAlpha maskAlpha;
        public TweenAlpha delayShow;
        public TweenPosition bodyPos;

        public delegate void OnHeroBtnClick(string guid);
        public OnHeroBtnClick m_GetCallback;
        public OnHeroBtnClick m_OpenCallback;
        [HideInInspector]
        public string guidOrHeroId = "";
        public GameObject tuJianBtns;
        protected override void DoOpen()
        {
            maskAlpha.ResetToBeginning();
            titlePos.ResetToBeginning();
            bodyAlpha.ResetToBeginning();
            delayShow.ResetToBeginning();
            bodyPos.ResetToBeginning();

            bodyPos.PlayForward();
            bodyAlpha.PlayForward();
            titlePos.PlayForward();
            maskAlpha.PlayForward();
            delayShow.PlayForward();

            logic = GetComponent<UIHeroLogic>();

            
            bool isGuid = false;
            bool mainHero = false;
            

            if (options.ContainsKey("guid"))
            {
                string guid = options["guid"];
                isGuid = true;
                guidOrHeroId = guid;

                //var heroid = StaticData.playerData.GetUIHero(guid).heroId;

                if (StaticData.playerData.GetUIHero(guid).heroData.heroClass == (int)HeroClass.Main_Actor_Man || StaticData.playerData.GetUIHero(guid).heroData.heroClass == (int)HeroClass.Main_Actor_Woman)
                {
                    mainHero = true;
                }
                else
                {
                    mainHero = false;
                }
                
            }
            else if (options.ContainsKey("heroId"))
            {
                string heroId = options["heroId"];
                isGuid = false;
                guidOrHeroId = heroId;
            }

            if (options.ContainsKey("showBtns"))
            {
                string showSwitchHeroStr = options["showBtns"];
                if (showSwitchHeroStr == "true")
                {
                    tuJianBtns.SetActive(true);
                }
                else
                {
                    tuJianBtns.SetActive(false);
                }
            }
            else
            {
                tuJianBtns.SetActive(false);
            }

            bool showSwitchHero = true;

            if (options.ContainsKey("showSwitchHero"))
            {
                string showSwitchHeroStr = options["showSwitchHero"];
                if (showSwitchHeroStr == "false")
                {
                    showSwitchHero = false;
                }
            }

            UIHero hero = null;
            UIHeroFormation fHero=null;

            if (isGuid)
            {
                foreach (var formation in StaticData.playerData.InstPlayerData.BattleCamps)
                {
                    if (formation.Value != null && formation.Value.hero != null
                        && formation.Value.hero.guid == guidOrHeroId)
                    {
                        StaticData.playerData.InstPlayerData.ResetAllFormationAttribute();
                        fHero = formation.Value;
                        break;
                    }
                }

                if (hero == null)
                {
                    hero = StaticData.playerData.InstPlayerData.GetUIHero(guidOrHeroId);
                }
                
            }
            else
            {
                guidOrHeroId = guidOrHeroId.Replace("frag_", "");

                hero = new UIHero(guidOrHeroId);
                ProtoBuf.Message.WarriorSimpleInfo data = new ProtoBuf.Message.WarriorSimpleInfo();
                data.level = 1;
                data.pos = 0;
                data.heroId = guidOrHeroId;
                data.baseProp = new ProtoBuf.Message.BaseProp();
                data.baseProp.antiCri = 0;
                data.baseProp.att = 0;
                data.baseProp.cri = 0;
                data.baseProp.criFactor = 0;
                data.baseProp.damageAdd = 0;
                data.baseProp.damageReduce = 0;
                data.baseProp.dodge = 0;
                data.breachedLevel = 1;
                hero.GenerateData();
                hero.InitData(data);
                
            }

            bool showUnloadHero = true;
            if (options.ContainsKey("showUnloadHero"))
            {
                if (options["showUnloadHero"] == "false")
                {
                    showUnloadHero = false;
                }
            }

            bool showOptBtn = true;

            if(!isGuid)
            {
                showSwitchHero = false;
                showUnloadHero = false;
                showOptBtn = false;
                
            }
            if (fHero != null)
            {
                logic.Init(fHero);
            }
            else
            {
                bool isMine = false;
                foreach (var __hero in StaticData.playerData.InstPlayerData.InstHeros)
                {
                    if(hero.heroId == __hero.Value.heroId)
                    {
                        isMine = true;
                        break;
                    }
                }
                logic.Init(hero, isMine);
            }
            logic.SetHero(showSwitchHero, showUnloadHero, showOptBtn, mainHero);
        }

        protected override void DoClose()
        {
            base.DoClose();
            logic.Close();
        }


        //获取
        public void OnGetBtnClick()
        {
            if (m_GetCallback != null)
                m_GetCallback(guidOrHeroId);
        }
        //打开图集
        public void OnOpenBtnClick()
        {
            if (m_OpenCallback != null)
                m_OpenCallback(guidOrHeroId);
        }
    }
}